package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.system.domain.InfoCategory;
import com.ruoyi.system.mapper.InfoCategoryMapper;
import com.ruoyi.system.service.IInfoCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 新闻栏目Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-02
 */
@Service
public class InfoCategoryServiceImpl implements IInfoCategoryService 
{
    @Autowired
    private InfoCategoryMapper infoCategoryMapper;

    /**
     * 查询新闻栏目
     * 
     * @param categoryid 新闻栏目主键
     * @return 新闻栏目
     */
    @Override
    public InfoCategory selectInfoCategoryByCategoryid(String categoryid)
    {
        return infoCategoryMapper.selectInfoCategoryByCategoryid(categoryid);
    }

    @Override
    public List<Map<String,Object>> findAll()
    {
        List<Map<String,Object>> infocategoryList = infoCategoryMapper.findAll();
        List<Map<String,Object>> returnList = new ArrayList<>();
        for (int i = 0; i < infocategoryList.size(); i++) {
            Map<String,Object> mmMap = infocategoryList.get(i);
            mmMap.put("checked","false");
            mmMap.put("open","false");
            mmMap.put("nocheck","false");

            returnList.add(mmMap);
        }
        return returnList;
    }

    @Override
    public List<Map<String,Object>> findAllValid()
    {
        List<Map<String,Object>> infocategoryList = infoCategoryMapper.findAllValid();
        List<Map<String,Object>> returnList = new ArrayList<>();
        for (int i = 0; i < infocategoryList.size(); i++) {
            Map<String,Object> mmMap = infocategoryList.get(i);
            mmMap.put("checked","false");
            mmMap.put("open","false");
            mmMap.put("nocheck","false");

            returnList.add(mmMap);
        }
        return returnList;
    }



    public List<Map<String, Object>> findByParentCategoryId(String ppParentCategoryId) {
        return infoCategoryMapper.findByParentCategoryId(ppParentCategoryId);
    }

    @Override
    public List<String> findAllXiajiCategoryid(String ppParentCategoryId) {
        List<Map<String, Object>> mmCategoryList = infoCategoryMapper.findByParentCategoryId(ppParentCategoryId);
        List<String> mmReturnList = new ArrayList<>();
        mmReturnList.add(ppParentCategoryId);
        for (int i = 0; i < mmCategoryList.size(); i++) {
            String mmCategoryid = mmCategoryList.get(i).get("categoryid") == null ? "" : mmCategoryList.get(i).get("categoryid").toString();
            //String mmParentCategoryid =  mmCategoryList.get(i).get("parentcategoryid") == null ? "" : mmCategoryList.get(i).get("parentcategoryid").toString();


            List<Map<String,Object>> mmXiajiCategoryList = infoCategoryMapper.findByParentCategoryId(mmCategoryid);
            if (mmXiajiCategoryList.size()>0){
                findAllXiajiCategoryid(mmCategoryid);
            }else {
                mmReturnList.add(mmCategoryid);
            }
        }

        return mmReturnList;
    }

    /**
     * 查询新闻栏目列表
     * 
     * @param infoCategory 新闻栏目
     * @return 新闻栏目
     */
    @Override
    public List<InfoCategory> selectInfoCategoryList(InfoCategory infoCategory)
    {
        return infoCategoryMapper.selectInfoCategoryList(infoCategory);
    }

    /**
     * 新增新闻栏目
     * 
     * @param infoCategory 新闻栏目
     * @return 结果
     */
    @Override
    public int insertInfoCategory(InfoCategory infoCategory)
    {
        return infoCategoryMapper.insertInfoCategory(infoCategory);
    }

    /**
     * 修改新闻栏目
     * 
     * @param infoCategory 新闻栏目
     * @return 结果
     */
    @Override
    public int updateInfoCategory(InfoCategory infoCategory)
    {
        return infoCategoryMapper.updateInfoCategory(infoCategory);
    }

    /**
     * 批量删除新闻栏目
     * 
     * @param categoryids 需要删除的新闻栏目主键
     * @return 结果
     */
    @Override
    public int deleteInfoCategoryByCategoryids(String categoryids)
    {
        return infoCategoryMapper.deleteInfoCategoryByCategoryids(Convert.toStrArray(categoryids));
    }

    /**
     * 删除新闻栏目信息
     * 
     * @param categoryid 新闻栏目主键
     * @return 结果
     */
    @Override
    public int deleteInfoCategoryByCategoryid(String categoryid)
    {
        return infoCategoryMapper.deleteInfoCategoryByCategoryid(categoryid);
    }
}
